<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\Level;
use app\common\model\User;
use think\Exception;

class LevelManager extends BaseManager
{

    /**
     * 获取级别列表
     * @param User|null $user
     * @param int $page
     * @param int $step
     * @return array
     */
    public static function List(User $user = null , int $page=1,int $step=10):array
    {
        $return = ['total'=>0,'totalPage'=>1,'currentPage'=>$page,'list'=>[]];
        if ($page <= 0){
            $page = 1;
        }
        $level = 0;
        try {

//            if ($user && $user->level){
//                $level = $user->level;
//            }
            $levelModel = new Level();
            $start = ($page - 1) * $step;

            $count = $levelModel
                ->where('delete',Level::DELETE_NO)
                ->where(function ($query) use($level){
                    if ($level){
                        return $query->where('vl_id',$level);
                    }
                })
                ->count();

            $levelInfo = $levelModel
                ->where('delete',Level::DELETE_NO)
                ->where(function ($query) use($level){
                    if ($level){
                        return $query->where('vl_id',$level);
                    }
                })
                ->order('vl_id desc')
                ->limit($start,$step)
                ->select();
            if ($levelInfo){
                foreach ($levelInfo as $k=>$v){
                    if ($v['vip_icon'] && strrpos($v['vip_icon'],'https') === false){
                        $levelInfo[$k]['vip_icon'] = cdnurl($v['vip_icon']);
                    }else{
                        config('app.api_domain').$v['vip_icon'];
                    }
                }
            }
            $pageData = page($count,$step,$page);

            $return['list'] = $levelInfo;
            $return = array_merge($return,$pageData);
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
            self::setData($return);
        }catch (Exception $exception){
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
            self::setData($return);
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

}